import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/res/lf_color.dart';
import 'package:largefriends/res/lf_double_ext.dart';

import '../config/lf_config.dart';
import '../res/lf_fonts.dart';
import 'lf_text_label_filed.dart';

Widget autoCompleteWidget({
  required Function(String value) onSelected,
  required Function(String value) onChanged,
  required FocusNode focusNode,
  required TextEditingController controller,
  TextStyle? inputStyle,
  TextStyle? hintStyle,
  String? hintText,
  bool enable = true,
}) {
  return RawAutocomplete<String>(
      onSelected: (s) {
        onSelected.call(s);
      },
      optionsViewBuilder: (
        BuildContext context,
        AutocompleteOnSelected<String> onSelected,
        Iterable<String> options,
      ) {
        return Column(
          children: [
            10.h.sbh,
            Align(
              alignment: Alignment.topLeft,
              child: Container(
                decoration: BoxDecoration(
                    color: color8F8F8F,
                    borderRadius: BorderRadius.all(Radius.circular(20.r))),
                constraints: BoxConstraints(
                    maxHeight: options.length * 40.h,
                    maxWidth: ScreenUtil().screenWidth - 2 * 16.w),
                child: CustomScrollView(
                  slivers: [
                    SliverList(
                      delegate: SliverChildBuilderDelegate(
                        (context, index) {
                          String text = options.elementAt(index);
                          return InkWell(
                              onTap: () {
                                int index = controller.text.indexOf('@');
                                if (index != -1) {
                                  String st =
                                      controller.text.substring(0, index);
                                  onSelected.call(st + text);
                                }
                              },
                              child: Padding(
                                padding: EdgeInsets.only(
                                    left: 16.w, top: 10.h, bottom: 10.h),
                                child: Text(
                                  text,
                                  style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 14.sp,
                                      fontFamily: fontRegular),
                                ),
                              ));
                        },
                        childCount: options.length,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        );
      },
      optionsBuilder: (TextEditingValue textEditingValue) {
        String text = textEditingValue.text;
        int index = text.indexOf('@');
        if (index == -1 || text.isEmail) {
          return [];
        } else {
          var subString = text.substring(index, text.length);
          if (subString.length < 2) {
            return emailOptions;
          } else {
            var list = List<String>.empty(growable: true);
            for (var element in emailOptions) {
              if (element.contains(subString)) {
                list.add(element);
              }
            }
            return list;
          }
        }
      },
      textEditingController: controller,
      focusNode: focusNode,
      fieldViewBuilder: (BuildContext context,
          TextEditingController textEditingController,
          FocusNode focusNode,
          VoidCallback onFieldSubmitted) {
        return LfTextLabelFiled(
          keyboardType: TextInputType.emailAddress,
          textEditingController: textEditingController,
          onChanged: (value) {
            onChanged.call(value);
          },
          inputFormatters: [
            FilteringTextInputFormatter.allow(RegExp('[A-Za-z0-9_@.]')),
          ],
          focusNode: focusNode,
          isPassword: false,
          hintText: hintText ?? "",
          hintStyle: hintStyle,
          inputStyle: inputStyle,
          enable: enable,
          prefixIcon: Icon(
            Icons.alternate_email,
            color: colorFF007F,
            size: 20.r,
          ),
        );
      });
}
